package com.google.code.jcrawler.output;

import java.io.File;
import java.util.Date;
import java.util.Set;

import com.redfin.sitemapgenerator.ChangeFreq;
import com.redfin.sitemapgenerator.WebSitemapGenerator;
import com.redfin.sitemapgenerator.WebSitemapUrl;

public class SitemapOutput {
    private String host;

    public SitemapOutput(String host) {
	this.host = host;
    }

    public void exportToFile(Set<String> urls, String dir) {
	File myDir = new File(dir);
	WebSitemapGenerator wsg;
	try {
	    wsg = WebSitemapGenerator.builder(host, myDir).allowMultipleSitemaps(false).build();

	    WebSitemapUrl url;

	    for (String link : urls) {
		if (link.equals(host)) {
		    url = new WebSitemapUrl.Options(link).lastMod(new Date()).priority(1.00).changeFreq(ChangeFreq.WEEKLY).build();
		} else {
		    url = new WebSitemapUrl.Options((!link.startsWith("http") ? host : "") + link).lastMod(new Date()).priority(0.80).changeFreq(ChangeFreq.WEEKLY).build();
		}
		wsg.addUrl(url);

	    }

	    wsg.write();
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

}
